/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model.BeanUltility;

import com.mysql.jdbc.ResultSetMetaData;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.persistence.Column;
import org.apache.commons.beanutils.BeanUtils;
import Model.DataUltility.*;
import Model.ObjectBean.*;
import Model.ObjectBean.ViewBean.PersonalInfo_FrDatabase;

/**
 *
 * @author chickencake1
 */
public class ResultsetMapper {
    public static void mapperResultsettoObject(ResultSet rs,Class output,Object bean) throws SQLException, IllegalAccessException, InvocationTargetException
    {
        //Cart_Info cart_Info= new Cart_Info();
        
        // Liet ke cac attribute của class ouput vao mang Field[]
        Field [] field=output.getDeclaredFields(); 
        ResultSetMetaData rsmd= (ResultSetMetaData) rs.getMetaData();
        while(rs.next())
        {
            int j;
             Object [] columnValue = new Object[20];
            for(int i=0;i<rsmd.getColumnCount();i++)
            {
                
               
                String columnName=rsmd.getColumnName(i+1);
                columnValue[i]=rs.getObject(i+1);
                System.out.println(columnValue[i]);
                
            }
            
            
            j=0;
            for(Field fil : field)
            {
             
            //Column column=fil.getAnnotation(Column.class);
            //System.out.println(column.name());
           // System.out.println(fil.getName());
            //System.out.println(columnValue[j].toString());

            BeanUtils.setProperty(bean,fil.getName(),columnValue[j]);
            j++;
            
            }
            
        }

    }
    public static void main(String []argv) throws SQLException, IllegalAccessException, InvocationTargetException
    {
        
        Cart_Info cart_Info= new Cart_Info();
        Resource re = new Resource();
        Data_Access access = new Data_Access();
        //String []data1={"K001","123456"};
       // String []data2={"K001","Khang","Nguyen","chickencake1@yahoo.com",
        //"51/9c","1","0983746056","Viet Nam","19-02-1990"};
        //String []data3={"khang","label","introduce","cover","price"};
        //access.insertInformation_AutoIncre("resource",data3);
       
      
        //access.insertInformation("login_account",data1);
       // access.insertInformation("personal_info", data2);
        PersonalInfo_FrDatabase b = new PersonalInfo_FrDatabase();
       ResultSet rs = access.getAllInformation("personal_info");
        mapperResultsettoObject(rs,PersonalInfo_FrDatabase.class,b);
        System.out.println("firstname="+b.getFirstname());
                
    }
}
